//
//  ICEEMIdCard.m
//  EasyMoney
//
//  Created by 邓乐 on 2020/1/9.
//  Copyright © 2020 zh. All rights reserved.
//

#import "ICEEMIdCard.h"
#import "ICEJavaScriptBridge.h"
#if !TARGET_OS_SIMULATOR
#import <OCRDetection/OCRDetection.h>
#endif

@implementation ICEEMIdCard

+ (instancetype)sharedSingleton {
    static ICEEMIdCard *_sharedSingleton = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //不能再使用alloc方法
        //因为已经重写了allocWithZone方法，所以这里要调用父类的分配空间的方法
        _sharedSingleton = [[super allocWithZone:NULL] init];
    });
    return _sharedSingleton;
}

// 防止外部调用alloc 或者 new
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [ICEEMIdCard sharedSingleton];
}

// 防止外部调用copy
- (id)copyWithZone:(nullable NSZone *)zone {
    return [ICEEMIdCard sharedSingleton];
}

// 防止外部调用mutableCopy
- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [ICEEMIdCard sharedSingleton];
}

-(void)getIdCard:(NSDictionary*)args
{
    ICEWebInfo* vcinfo = [args objectForKey:@"vcinfo"];

    if (vcinfo.vc) {
#if !TARGET_OS_SIMULATOR
        MGOCRManager *liveManager = [MGOCRManager share];
        [liveManager startOCRDetectionViewController:vcinfo.vc ClientID:@"4ea103e2-452f-48b3-8d41-a16b539f8603" ClienclientSecrettID:@"KnutuqZCb9WhqbbVbRFRmA7AiSkhIOkM" UIconfiguration:nil finish:^(UIImage *image, UIViewController *vc, NSDictionary *info) {
            //success
            NSString* base64EncodedString = [image imageBase64StringWithMime];
            NSDictionary* ret = nil;
            if (base64EncodedString.length > 0) {
                ret = @{
                    @"status":@"success",
                    @"image":base64EncodedString,
                    @"supplier":@"shangtang"
                };
            }
            else
            {
                ret = @{
                    @"status":@"error_other",
                };
            }
            [ICEJavaScriptBridge sendReslutToJS:ret callInfo:args];
            [vc dismissViewControllerAnimated:YES completion:nil];

        } error:^(MGLivenessDetectionFailedType errorType, UIViewController * _Nonnull vc) {
            NSDictionary* ret = nil;
            {
                ret = @{
                    @"status":@"error_other",
                    @"errorCode":@(errorType)
                };
            }
            [ICEJavaScriptBridge sendReslutToJS:ret callInfo:args];
            [vc dismissViewControllerAnimated:YES completion:nil];
        } cancel:^(UIViewController *vc) {
            NSDictionary* ret = nil;
            {
                ret = @{
                    @"status":@"error_cancel",
                };
            }
            [ICEJavaScriptBridge sendReslutToJS:ret callInfo:args];
            [vc dismissViewControllerAnimated:YES completion:nil];
        }];
#endif
    }
}

@end
